﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeManagementSystem
{
    public partial class Main : System.Web.UI.MasterPage
    {
        private User _user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["ASP.NET_SessionId"] != null && Request.Cookies["sessionId"] != null)
            {
                _user = Global.inputValid.GetUser(Request.Cookies["ASP.NET_SessionId"].Value, Request.Cookies["sessionId"].Value);
                userLogin.Text = "Welcome ";
                if (_user == null)
                    userLogin.Text = "";
                else
                    userLogin.Text += _user.Name + " " + _user.Surname;
            }
        }

        protected void logout_OnClick(object sender, EventArgs e)
        {
            if (_user != null)
                Global.inputValid.dao.SetUserSessionId(_user, null);
            Response.Cookies["sessionId"].Value = "empty";
            Response.Redirect("/Login.aspx");
        }

        public User user
        {
            get { return _user; }
            set { _user = value; } 
        }

        public void ShowException(string exceptionTxt)
        {
            ScriptManager.RegisterStartupScript(this, GetType(),
            "showExceptionPop", "showExceptionPop('" + exceptionTxt + "');", true);
        }

        public User GetCreatorUser()
        {
            return Global.inputValid.GetUser(Request.Cookies["ASP.NET_SessionId"].Value,
                Request.Cookies["sessionId"].Value);
        }
    }
}